package com.zheng.redpacket.server.serivce.Impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zheng.redpacket.mdg.mapper.SysLogMapper;
import com.zheng.redpacket.mdg.model.SysLog;
import com.zheng.redpacket.server.dto.UserLoginDto;
import com.zheng.redpacket.server.serivce.SysLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
@EnableAsync
public class SysLogServiceImpl implements SysLogService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SysLogService.class);
    @Resource
    private SysLogMapper sysLogMapper;
    @Autowired
    private ObjectMapper objectMapper;
    @Override
    @Async
    public void recordLog(UserLoginDto dto) {
        try {
            SysLog log = new SysLog();
            log.setUserId(dto.getUserId());
            log.setModule("用户登录模块");
            log.setData(objectMapper.writeValueAsString(dto));
            log.setMemo("用户登录成功");
            log.setCreateTime(new Date());
            sysLogMapper.insert(log);
        } catch (JsonProcessingException e) {
            LOGGER.error("异常：{}", dto, e.fillInStackTrace());
        }

    }
}
